/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JFreeChartExtensions.storable;

import JFreeChartExtensions.chartextensions.AbstractChartPanelExtension;
import JFreeChartExtensions.Collections.DatasetCollection;
import JFreeChartExtensions.ExtendableChartPanel;
import JFreeChartExtensions.containers.InternalFrameWithChart.InternalFrameWithChart;
import JFreeChartExtensions.chartextensions.AbstractStorableChartPanelExtension;
import JFreeChartExtensions.renderers.XYErrorRendererExtended;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import javax.swing.JInternalFrame;
import org.jfree.data.xy.XYIntervalSeries;

/**
 *
 * @author Nobody
 */
public class StorableChartFrame implements Serializable {

    private List<StorableSeriesObject> list = new LinkedList<StorableSeriesObject>();
    private List<StorableRendererObject> renderers = new LinkedList<StorableRendererObject>();
    private List<StorableExtenstionObject> extenstions = new LinkedList<StorableExtenstionObject>();
    private String name;
    protected Rectangle bounds;
    protected int layer;

    public Rectangle getBounds() {
        return bounds;
    }

    public int getLayer() {
        return layer;
    }

    public List<StorableRendererObject> getStorableRenderersList() {
        return renderers;
    }

    public StorableChartFrame(JInternalFrame frame) {
        this.bounds = frame.getBounds();
        this.layer = frame.getLayer();

        name = frame.getTitle();
     //   if (canBeStored(frame)) {

            InternalFrameWithChart internalFrameWithChart = (InternalFrameWithChart) frame;
            bounds = internalFrameWithChart.getBounds();
            storeSeries(internalFrameWithChart);
            storeSeriesGraphicalSettings(internalFrameWithChart);
            storeChartExtensions(internalFrameWithChart);
       // }
    }

  /*  private boolean canBeStored(JInternalFrame frame) {
        return (frame instanceof IChartInternalFrame && !(frame instanceof INotStorableInternalFrame));
    }*/

    private void storeSeries(InternalFrameWithChart internalFrameWithChart) {
        DatasetCollection collection = internalFrameWithChart.getChart().getDatasetCollection();
        //for (Entry<Integer, XYReggresionSeriesCollection> entry : internalFrameWithChart.getDatasetCollection().getDataSeriesCollections().entrySet()) {
        //  XYReggresionSeriesCollection seriesCollection = entry.getValue();
        for (XYIntervalSeries series : collection.getSeriesList()) {
            StorableSeriesObject object = new StorableSeriesObject(series);
            list.add(object);
        }
        // }
    }

    private void storeSeriesGraphicalSettings(InternalFrameWithChart internalFrameWithChart) {
        for (Entry<Integer, XYErrorRendererExtended> entry : internalFrameWithChart.getChart().getDatasetCollection().getAllRendererCollections().get(0).getRenderersCollections().entrySet()) {
            XYErrorRendererExtended renderer = entry.getValue();
            int i = -1;
            while (entry.getValue().getSeriesPaint(++i) != null) {
                this.renderers.add(new StorableRendererObject(i,
                        entry.getKey(),
                        renderer.getSeriesVisible(i),
                        renderer.getSeriesLinesVisible(i),
                        renderer.getSeriesShapesVisible(i),
                        renderer.getSeriesXError(i),
                        renderer.getSeriesYError(i),
                        (Color) renderer.getSeriesPaint(i)));
            }

        }



        /*

        ExtendableChartPanel chartPanel = internalFrameWithChart.getChart();
        int size = chartPanel.getXYPlot().getRendererCount();
        for (int i = 0; i < size; ++i) {
        XYErrorRendererExtended renderer = (XYErrorRendererExtended) chartPanel.getXYPlot().getRenderer();
        this.renderers.add(new StorableRendererObject(i,
        renderer.getSeriesVisible(i),
        renderer.getSeriesLinesVisible(i),
        renderer.getSeriesShapesVisible(i),
        renderer.getSeriesXError(i),
        renderer.getSeriesYError(i),
        (Color) renderer.getSeriesPaint(i)));

        }*/

    }

    public List<StorableSeriesObject> getList() {
        return list;
    }

    public String getName() {
        return name;
    }

    public List<StorableExtenstionObject> getExtenstions() {
        return extenstions;
    }

    private void storeChartExtensions(InternalFrameWithChart internalFrameWithChart) {
        ExtendableChartPanel chartPanel = internalFrameWithChart.getChart();
        for (AbstractChartPanelExtension extension : chartPanel.getChartPanelExtensions().values()) {
            if (extension instanceof AbstractStorableChartPanelExtension) {
                extenstions.add(((AbstractStorableChartPanelExtension) extension).storeExtension());

            }
        }
    }
}
